package com.dmcloud.admin;

import com.dmcloud.admin.entity.Credentials;
import com.dmcloud.admin.entity.TokenInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by xiaodao
 * date: 2019/11/7
 */
@SpringBootApplication
@RestController
@Slf4j
@EnableZuulProxy
public class AdminApplication {
  public static void main(String[] args) {
    SpringApplication.run(AdminApplication.class,args);
  }

  private RestTemplate restTemplate = new RestTemplate();

  /**
   * 根据前端的用户名和密码 去请求网关服务获取token
   * 获取token成功就代表成功了.密码模式
   * @param credentials
   * @param request
   */
  @PostMapping("/login11")
  public void login(@RequestBody Credentials credentials, HttpServletRequest request){
    String oauthServiceUrl=  "http://localhost:9070/token/oauth/token";
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    //appid appsecret
    httpHeaders.setBasicAuth("admin","123456");

    MultiValueMap<String,String> multiValueMap =new LinkedMultiValueMap<>();
    multiValueMap.add("username",credentials.getUsername());
    multiValueMap.add("password",credentials.getPassword());
    multiValueMap.add("grant_type","password");

    HttpEntity<MultiValueMap<String,String>> entity =  new HttpEntity<>(multiValueMap,httpHeaders);
    ResponseEntity<TokenInfo> responseEntity =restTemplate.exchange(oauthServiceUrl, HttpMethod.POST,entity,TokenInfo.class);
    log.info("tokenInfo: "+ responseEntity.getBody().toString());
    request.getSession().setAttribute("token",responseEntity.getBody());

  }

  /**
   * 授权码模式 1.先去请求授权服务器-->返回redirect_url +code 码
   * 2.拿到code码+clientid  secret 去授权服务器-获取code
   *3.获取到token 跳转掉index.html
   * 4.判断登录了没有
   * @Link me 吧token 返回给页面证明登录了.
   * @param code
   * @param state
   * @param request
   * @param response
   * @throws IOException
   */
  @GetMapping("/oauth/callback")
  public void callback(@RequestParam String code , String state, HttpServletRequest request
  , HttpServletResponse response) throws IOException {
    log.info("state: {}",state);
    String oauthServiceUrl=  "http://localhost:9070/token/oauth/token";
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    //appid appsecret
    httpHeaders.setBasicAuth("admin","123456");

    MultiValueMap<String,String> multiValueMap =new LinkedMultiValueMap<>();
    multiValueMap.add("code",code);
    multiValueMap.add("grant_type","authorization_code");
    multiValueMap.add("redirect_url","http://localhost:8080/oauth/callback");

    HttpEntity<MultiValueMap<String,String>> entity =  new HttpEntity<>(multiValueMap,httpHeaders);
    ResponseEntity<TokenInfo> responseEntity =restTemplate.exchange(oauthServiceUrl, HttpMethod.POST,entity,TokenInfo.class);
    log.info("tokenInfos授权码模式返回: "+ responseEntity.getBody().toString());
    request.getSession().setAttribute("token",responseEntity.getBody().init());
    response.sendRedirect("/");

  }

  @GetMapping("/me")
  public TokenInfo me(HttpServletRequest request
    , HttpServletResponse response){
    TokenInfo token = (TokenInfo) request.getSession().getAttribute("token");
    return token;
  }


  @PostMapping("/logout")
  public void logout(HttpServletRequest request){
    request.getSession().invalidate();
  }
}

